﻿@using System.Text
@using KRM.Core
@model List<CustomerEmailHistory>

@foreach (var item in Model)
{
    var kybo = "";
    var action = "";
    var mes = new StringBuilder();
    var attachmentsList = new List<string>();
    string fileName = "";
    try
    {
        string folder = tblCompanyVariable.Folders_CustomerEmails + "\\" + item.DateSent.Year + "\\" + item.DateSent.Month + "\\" + item.DateSent.Day;

        string[] AllFiles = Directory.GetFiles(folder, item.CustomerEmailHistoryID.ToString() + "*");
        if (AllFiles.Any())
        {
            fileName = AllFiles[0];
        }
        string attachment = item.EmailAttachment != null ? item.EmailAttachment.FileName : "";
        attachmentsList = attachment.Split(';').ToList();
        string[] allLine = System.IO.File.ReadAllLines(fileName);
        if (allLine.Length > 0)
        {
            kybo = allLine[0].ToString().Replace("KyboInfo: ", "").Replace("<p/>", "");
            action = allLine[1].ToString().Replace("Action: ", "").Replace("<p/>", "");
        }
        for (int i = 2; i < allLine.Length; i++)
        {
            mes.AppendLine(allLine[i]);
        }
    }
    catch { }
    <tr>
        <td>
            <a href="#email@(item.CustomerEmailHistoryID)" role="button" class="btn" data-toggle="modal" style="padding: 5px 3px">...
            </a>
            <div id="email@(item.CustomerEmailHistoryID)" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" data-width="800">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 id="myModalLabel">Order Message</h4>
                </div>
                <div class="modal-body">
                    <div class="row-fluid">
                        <div class="span6">
                            <div class="row-fluid">
                                <div class="span4">Kybo Info</div>
                                <div class="span8">
                                    <input type="text" class="input-block-level" value="@kybo"/>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="span4">Action</div>
                                <div class="span8">
                                    <input type="text" class="input-block-level" value="@action"/>
                                </div>
                            </div>
                        </div>
                        <div class="span6">
                            <div class="span4 text-right">
                                Attachment(s): 
                            </div>
                            <div class="span8">
                                @foreach (string t in attachmentsList)
                                {
                                    Html.Raw("<p>- " + t + "</p>");
                                }
                            </div>
                        </div>
                    </div>
                    <iframe width="100%" height="250px" frameborder="0" src="/OrderView/GetEmailHtml?path=@Server.UrlEncode(fileName)"></iframe>
                </div>
            </div>
        </td>
        <td>@item.OrderID</td>
        <td>@item.Fromaddress</td>
        <td>@item.Toaddress</td>
        <td>@item.DateSent</td>
        <td style="text-align: center">
            <input type="checkbox" disabled="disabled" @(item.AttachmentID != null ? "checked=checked" : "") />
        </td>
    </tr>
}
